#include <vector>
#include <string>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <cstdio>
#include <list>
using namespace std;

// 长度最小的子数组
class Solution
{
public:
    int minSubArrayLen(int target, vector<int>& nums)
    {
        int n = nums.size(), len = INT_MAX, sum = 0;
        for (int left = 0, right = 0; right < n; right++)
        {
            sum += nums[right];
            while (sum >= target)
            {
                len = min(len, right - left + 1);
                sum -= nums[left++];
            }
        }
        return len == INT_MAX ? 0 : len;
    }
};

// 无重复字符的最长子串
class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int hash[128] = { 0 };
        int left = 0, right = 0, ret = 0, n = s.size();
        while (right < n)
        {
            hash[s[right]]++;//进入窗口
            while (hash[s[right]] > 1)
            {
                hash[s[left++]]--;//出窗口
            }
            ret = max(ret, right - left + 1);
            right++;
        }
        return ret;
    }
};

// 最大连续1的个数III
class Solution {
public:
    int longestOnes(vector<int>& nums, int k) {
        int ret = 0, zero = 0;
        for (int left = 0, right = 0; right < nums.size(); right++)
        {
            if (nums[right] == 0) zero++;// 进窗口
            while (zero > k)
            {
                if (nums[left++] == 0) zero--;// 出窗口
            }
            ret = max(ret, right - left + 1);
        }
        return ret;
    }
};

// 将x减到0的最小操作数
class Solution {
public:
    int minOperations(vector<int>& nums, int x) {
        int sum = 0, n = nums.size(), ret = -1;
        for (int e : nums) sum += e;
        int target = sum - x;
        if (target < 0) return -1;
        for (int left = 0, right = 0, tmp = 0; right < n; right++)
        {
            tmp += nums[right];//进窗口
            while (tmp > target) // 判断
                tmp -= nums[left++];//出窗口
            if (target == tmp)
                ret = max(ret, right - left + 1);//更新
        }
        return ret == -1 ? ret : n - ret;
    }
};

// 水果成篮
class Solution {
public:
    int totalFruit(vector<int>& fruits) {
        // unordered_map<int, int> hash;
        int hash[100001] = {0};
        int ret = 0, kinds = 0;
        for (int left = 0, right = 0; right < fruits.size(); right++)
        {
            if (hash[fruits[right]] == 0) kinds++;
            hash[fruits[right]]++;//进窗口
            while (kinds > 2)//判断
            {
                hash[fruits[left]]--;//出窗口
                if (hash[fruits[left]] == 0) kinds--;
                // if (hash[fruits[left]] == 0)
                    // hash.erase(fruits[left]);

                left++;
            }
            ret = max(ret, right - left + 1);
        }
        return ret;
    }
};

// 找到字符串中所有字母的易位词
class Solution {
public:
    vector<int> findAnagrams(string s, string p) {
        int hash1[26] = {0};
        for (auto &ch : p) hash1[ch - 'a']++;
        int hash2[26] = {0};
        vector<int> ret;
        for (int left = 0, right = 0, count = 0; right < s.size(); right++)
        {
            char ch = s[right];
            if (++hash2[ch - 'a'] <= hash1[ch - 'a']) count++;// 进窗口
            if (right - left + 1 > p.size())
            {
                char ch = s[left++]; // 出窗口
                if (hash2[ch - 'a']-- <= hash1[ch - 'a']) count--;
            }
            if (count == p.size())
            ret.push_back(left);
        }
        return ret;
    }
};

// 串联所有单词的子串
class Solution {
public:
    vector<int> findSubstring(string s, vector<string>& words) {
        unordered_map<string, int> hash1;
        vector<int> ret;
        for (auto &str : words) hash1[str]++;
        int len = words[0].size();
        for (int i = 0; i < len; i++)
        {
            unordered_map<string, int> hash2;
            for (int left = i, right = i, count = 0; right + len <= s.size(); right += len)
            {
                string in = s.substr(right, len);
                if (hash1.count(in) && ++hash2[in] <= hash1[in]) count++; // 进窗口
                if (right - left + 1 > len * words.size())
                {
                    string out = s.substr(left, len);
                    if (hash1.count(out) && hash2[out]-- <= hash1[out]) count--;// 出窗口
                    left += len;
                }
                if (count == words.size()) ret.push_back(left);
            }
        }
        return ret;
    }
};

// 最小覆盖子串
class Solution {
public:
    string minWindow(string s, string t) {
        int hash1[128] = {0};
        int kinds = 0;// t中字符种类个数
        for (auto ch : t)
            if (hash1[ch]++ == 0) kinds++;
        int hash2[128] = {0};
        int pos = -1, len = INT_MAX;// 记录构造最小字符串的位置和长度
        for (int left = 0, right = 0, count = 0; right < s.size(); right++)
        {
            char in = s[right];
            if (++hash2[in] == hash1[in]) count++;// 进窗口
            while (count == kinds) // 判断count是否一直满足kinds
            {
                if (right - left + 1 < len)// 仅当最小长度需要更新时才需要记录pos位置
                {
                    len = right - left + 1;
                    pos = left;
                }
                char out = s[left++];
                if (hash2[out]-- == hash1[out]) count--;// 出窗口
            }
        }
        return pos == -1 ? "" : s.substr(pos, len);
    }
};